package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("banner")
@Slf4j
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Value("${mypath}")
    private String path;
//    查询全部加分页
    @RequestMapping("selectAll")
    @ResponseBody
    @LogAnnotation(content="查询所有轮播图数据",type="查询")
    public Map selectAll(Integer page,Integer limit,String name){
        Map map = new HashMap();
        PageInfo<Banner> pageInfo = bannerService.selectAll(page, limit, name);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    // 上传图片
    @RequestMapping("upload")
    @ResponseBody
    @LogAnnotation(content="添加轮播图封面",type="添加")
    public Map upload(MultipartFile file)throws Exception{
        Map map = new HashMap();
        String newName = UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File(path,newName));
        log.info("新文件名字"+newName);
        map.put("newName",newName);
        return map;
    }


//    添加一个对象
    @RequestMapping("addBanner")
    @ResponseBody
    @LogAnnotation(content="添加轮播图",type="添加")
    public Map addBanner(Banner banner){
        Map map = new HashMap();
        try {
            bannerService.insert(banner);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
            e.printStackTrace();
        }
        return map;
    }
//    假删除   批量删除和单个删除 用于一个方法
    @RequestMapping("falseDeletes")
    @ResponseBody
    @LogAnnotation(content="删除轮播图",type="删除")
    public Map falseDeletes(Integer id){
        Map map = new HashMap();
        try {
            bannerService.falseDeletes(id);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
            e.printStackTrace();
        }
        return map;
    }
//    查询一个对象用于回显
    @RequestMapping("selectOne")
    @ResponseBody
    @LogAnnotation(content="查询单个图片",type="查询")
    public Banner selectOne(Integer id){
        Banner banner = bannerService.selectOne(id);
        return banner;
    }
    //修改对象
    @RequestMapping("updateBanner")
    @ResponseBody
    @LogAnnotation(content="更新图片信息",type="更新")
    public Map updateBanner(Banner banner){
        Map map = new HashMap();
        try {
            bannerService.update(banner);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
            e.printStackTrace();
        }
        return map;
    }
}
